<div class="doc-item" id="doc-699"><h2 class="doc-item-name">condp<span class="namespace">(clojure.core)</span></h2><p class="macro">Macro</p><ul class="var-arglists"><li>(<span class="var-name">condp</span> pred expr &amp; clauses)</li></ul><div class="var-docstring"><p>Takes a binary predicate, an expression, and a set of clauses.
  Each clause can take the form of either:</p><p>test-expr result-expr</p><p>test-expr :&gt;&gt; result-fn</p><p>Note :&gt;&gt; is an ordinary keyword.</p><p>For each clause, (pred test-expr expr) is evaluated. If it returns
  logical true, the clause is a match. If a binary clause matches, the
  result-expr is returned, if a ternary clause matches, its result-fn,
  which must be a unary function, is called with the result of the
  predicate as its argument, the result of that call being the return
  value of condp. A single default expression can follow the clauses,
  and its value will be returned if no clause matches. If no default
  expression is provided and no clause matches, an
  IllegalArgumentException is thrown.</p></div><div class="var-source"><pre>; clojure/core.clj:3895
(defmacro condp
  [pred expr &amp; clauses]
  (let [gpred (gensym &quot;pred__&quot;)
        gexpr (gensym &quot;expr__&quot;)
        emit (fn emit [pred expr args]
               (let [[[a b c :as clause] more]
                       (split-at (if (= :&gt;&gt; (second args)) 3 2) args)
                       n (count clause)]
                 (cond
                  (= 0 n) `(throw (IllegalArgumentException. (str &quot;No matching clause: &quot; ~expr)))
                  (= 1 n) a
                  (= 2 n) `(if (~pred ~a ~expr)
                             ~b
                             ~(emit pred expr more))
                  :else `(if-let [p# (~pred ~a ~expr)]
                           (~c p#)
                           ~(emit pred expr more)))))
        gres (gensym &quot;res__&quot;)]
    `(let [~gpred ~pred
           ~gexpr ~expr]
       ~(emit gpred gexpr clauses))))</pre></div><div class="license"><p>Copyright (c) Rich Hickey. All rights reserved.</p><p>The use and distribution terms for this software are covered by the <a href="http://opensource.org/licenses/eclipse-1.0.php">Eclipse Public License 1.0</a>, which can be found in the file
          epl-v10.html at the root of this distribution. By using this software
          in any fashion, you are agreeing to be bound by the terms of this
          license. You must not remove this notice, or any other, from this
          software.</p></div></div>